//
//  GKZYUserResetPasswordViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/3/14.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYUserResetPasswordViewController.h"

@interface GKZYUserResetPasswordViewController ()

@end

@implementation GKZYUserResetPasswordViewController

- (void)loadView
{
    self.resetPasswordView = [[GKZYUserResetPasswordView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.resetPasswordView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.resetPasswordView.userName.LTTextField.text = [AVUser currentUser].username;
    [self.resetPasswordView.buttonSure addTarget:self action:@selector(buttonSureAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonSureAction:(UIButton *)sender
{
    [self textFieldResignFirstResponder];
    [self getUserMessage];
    if ([self.strUserNewPassword isEqualToString:self.strUserNewPasswordSure]) {
        if (self.strUserNewPassword.length) {
            [[AVUser currentUser] updatePassword:self.strUserPrimaryPassword newPassword:self.strUserNewPassword block:^(id object, NSError *error) {
                if (!error) {
                    DataSaveSucceedView *dataSaveSucceedView = [[DataSaveSucceedView alloc] initWithFrame:[UIScreen mainScreen].bounds];
                    [dataSaveSucceedView addAnimationOnViewController:self title:@"修改成功"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        GKZYUserLoginViewController *loginVC = [[GKZYUserLoginViewController alloc] init];
                        [self.navigationController pushViewController:loginVC animated:YES];
                    });
                }else{
                    if (error.code == 210) {
                        [self addPrimaryPasswordWrongErrorAlertController];
                    }
                    NSLog(@"%@",error);
                }
            }];
        }else{
            [self addPasswordIsNilErrorAlertController];
        }
    }else{
        [self addPasswordIsDifferentError];
    }
}

- (void)textFieldResignFirstResponder
{
    [self.resetPasswordView.userName.LTTextField resignFirstResponder];
    [self.resetPasswordView.userPrimaryPassword.LTTextField resignFirstResponder];
    [self.resetPasswordView.userNewPassword.LTTextField resignFirstResponder];
    [self.resetPasswordView.userNewPasswordSure.LTTextField resignFirstResponder];
}

- (void)getUserMessage
{
    self.strUserName = self.resetPasswordView.userName.LTTextField.text;
    self.strUserPrimaryPassword = self.resetPasswordView.userPrimaryPassword.LTTextField.text;
    self.strUserNewPassword = self.resetPasswordView.userNewPassword.LTTextField.text;
    self.strUserNewPasswordSure = self.resetPasswordView.userNewPasswordSure.LTTextField.text;
}

- (void)addPrimaryPasswordWrongErrorAlertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"原密码错误!" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)addPasswordIsDifferentError
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"两次密码输入不一致!" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)addPasswordIsNilErrorAlertController
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"新密码不能为空!" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:alertAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
